import cv2
from _0work_tools import cv_show
"""
@Project: pythonPro1
@Name: _02threshold.py
@Author: linxin_liu
@Date: 2022/10/15 9:18
阈值处理
"""


def thresh_operate(thresh_value, img, types):
    # 阈值、图片、阈值处理类型
    if types == 'Binary':  # 最普通的阈值处理，大于阈值为255(白色)，小于等于阈值为黑色。
        for i in range(0, len(img)):
            for j in range(0, len(img[0])):
                if img[i][j] > thresh_value:
                    img[i][j] = 255
                else:
                    img[i][j] = 0
    elif types == 'Binary_invert':
        for i in range(0, len(img)):
            for j in range(0, len(img[0])):
                if img[i][j] > thresh_value:
                    img[i][j] = 0
                else:
                    img[i][j] = 255
    return img


if __name__ == '__main__':
    img_original = cv2.imread('D:/tools/image_operation/cat.png', cv2.IMREAD_GRAYSCALE)
    img_thresh = thresh_operate(127, img_original, 'Binary')
    print(img_thresh)
    cv_show('a', img_thresh, 8000)
